package com.xyazm.inventory.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xyazm.common.dao.BaseDAO;
import com.xyazm.inventory.domain.entity.ProductStockLogDO;
import com.xyazm.inventory.mapper.ProductStockLogMapper;
import org.springframework.stereotype.Repository;

/**
 * <p>
 * 库存扣减日志表 Mapper 接口
 * </p>
 */
@Repository
public class ProductStockLogDAO extends BaseDAO<ProductStockLogMapper, ProductStockLogDO> {


    /**
     * 查询库存扣减日志
     * @param orderId
     * @param skuCode
     * @return
     */
    public ProductStockLogDO getLog(String orderId, String skuCode) {
        LambdaQueryWrapper<ProductStockLogDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductStockLogDO::getOrderId,orderId)
                .eq(ProductStockLogDO::getSkuCode,skuCode);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 更新库存日志状态
     * @return
     */
    public Boolean updateStatus(Long id,Integer status) {
        LambdaUpdateWrapper<ProductStockLogDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ProductStockLogDO::getId,id)
                .set(ProductStockLogDO::getStatus,status);
        return update(updateWrapper);
    }


    /**
     * 查询sku库存最近一笔扣减日志
     * @author xuyou
     * 2022-08-28 21:59
     * @param skuCode
    */
    public ProductStockLogDO getLatestOne(String skuCode) {
        LambdaQueryWrapper<ProductStockLogDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(ProductStockLogDO::getSkuCode, skuCode)
                .orderByDesc(ProductStockLogDO::getId).last("limit 1");
        return baseMapper.selectOne(queryWrapper);
    }
}
